//
//  UPContactTextFieldCell.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/27.
//

#import "UPContactTextFieldCell.h"

@interface UPContactTextFieldCell ()

@property (nonatomic, strong) UIImageView *containerImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *relateTextFieldBgView;
@property (nonatomic, strong) UITextField *relateTextField;
@property (nonatomic, strong) UIImageView *relateArrowImgView;
@property (nonatomic, strong) UIButton *relateArrowButton;

@property (nonatomic, strong) UIView *contactTextFieldBgView;
@property (nonatomic, strong) UITextField *contactTextField;
@property (nonatomic, strong) UIImageView *contactArrowImgView;
@property (nonatomic, strong) UIButton *contactArrowButton;

@end

@implementation UPContactTextFieldCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    [self.containerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(40);
        make.leading.trailing.equalTo(self).inset(20);
    }];
    
    [self.relateTextFieldBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(2);
        make.left.right.equalTo(self.contentView).inset(16);
        make.height.mas_equalTo(44);
    }];
    
    [self.relateTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.relateTextFieldBgView);
        make.left.right.equalTo(self.relateTextFieldBgView).inset(12);
    }];
    
    [self.relateArrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.relateTextFieldBgView);
    }];
    
    [self.relateArrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.relateTextFieldBgView);
        make.right.equalTo(self.relateTextFieldBgView).offset(-10);
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(22);
    }];
    
    [self.contactTextFieldBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.relateTextFieldBgView.mas_bottom).offset(8);
        make.left.right.equalTo(self.contentView).inset(16);
        make.height.mas_equalTo(self.relateTextFieldBgView.mas_height);
    }];
    
    [self.contactTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contactTextFieldBgView);
        make.left.right.equalTo(self.contactTextFieldBgView).inset(7);
    }];
    
    [self.contactArrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contactTextFieldBgView);
    }];
    
    [self.contactArrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contactTextFieldBgView);
        make.right.equalTo(self.contactTextFieldBgView).offset(-10);
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(22);
    }];
}

- (UIImageView *)containerImageView {
    if (!_containerImageView) {
        _containerImageView = [[UIImageView alloc] init];
        _containerImageView.contentMode = UIViewContentModeScaleAspectFit;
        _containerImageView.image = [UIImage imageNamed:@"contact_bg"];
        _containerImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_containerImageView];
    }
    return _containerImageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = [UIColor colorWithHexString:@"000000"];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.numberOfLines = 1;
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UIImageView *)relateArrowImgView {
    if (!_relateArrowImgView) {
        _relateArrowImgView = [[UIImageView alloc] init];
        _relateArrowImgView.contentMode = UIViewContentModeScaleAspectFit;
        _relateArrowImgView.image = [UIImage imageNamed:@"arrow_forward"];
        _relateArrowImgView.userInteractionEnabled = YES;
        [self.contentView addSubview:_relateArrowImgView];
    }
    return _relateArrowImgView;
}

- (UIButton *)relateArrowButton {
    if (!_relateArrowButton) {
        _relateArrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_relateArrowButton addTarget:self action:@selector(clickRelateArrowButton) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_relateArrowButton];
    }
    return _relateArrowButton;
}

- (UITextField *)relateTextField {
    if (!_relateTextField) {
        _relateTextField = [[UITextField alloc] init];
        _relateTextField.font = [UIFont systemFontOfSize:13];
        _relateTextField.textColor = [UIColor colorWithHexString:@"000000"];
        _relateTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self.contentView addSubview:_relateTextField];
    }
    return _relateTextField;
}

- (UIView *)relateTextFieldBgView {
    if (!_relateTextFieldBgView) {
        _relateTextFieldBgView = [[UIView alloc] init];
        _relateTextFieldBgView.layer.borderColor = [UIColor colorWithHexString:@"000000"].CGColor;
        _relateTextFieldBgView.layer.borderWidth = 1.0;
        _relateTextFieldBgView.layer.masksToBounds = true;
        _relateTextFieldBgView.layer.cornerRadius = 22;
        _relateTextFieldBgView.userInteractionEnabled = YES;
        [self.contentView addSubview:_relateTextFieldBgView];
    }
    return _relateTextFieldBgView;
}

- (UIImageView *)contactArrowImgView {
    if (!_contactArrowImgView) {
        _contactArrowImgView = [[UIImageView alloc] init];
        _contactArrowImgView.contentMode = UIViewContentModeScaleAspectFit;
        _contactArrowImgView.image = [UIImage imageNamed:@"arrow_forward"];
        _contactArrowImgView.userInteractionEnabled = YES;
        [self.contentView addSubview:_contactArrowImgView];
    }
    return _contactArrowImgView;
}

- (UIButton *)contactArrowButton {
    if (!_contactArrowButton) {
        _contactArrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_contactArrowButton addTarget:self action:@selector(clickContactArrowButton) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_contactArrowButton];
    }
    return _contactArrowButton;
}

- (UITextField *)contactTextField {
    if (!_contactTextField) {
        _contactTextField = [[UITextField alloc] init];
        _contactTextField.font = [UIFont systemFontOfSize:13];
        _contactTextField.textColor = [UIColor colorWithHexString:@"000000"];
        _contactTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        [self.contentView addSubview:_contactTextField];
    }
    return _contactTextField;
}

- (UIView *)contactTextFieldBgView {
    if (!_contactTextFieldBgView) {
        _contactTextFieldBgView = [[UIView alloc] init];
        _contactTextFieldBgView.layer.borderColor = [UIColor colorWithHexString:@"000000"].CGColor;
        _contactTextFieldBgView.layer.borderWidth = 1.0;
        _contactTextFieldBgView.layer.masksToBounds = true;
        _contactTextFieldBgView.layer.cornerRadius = 22;
        _contactTextFieldBgView.userInteractionEnabled = YES;
        [self.contentView addSubview:_contactTextFieldBgView];
    }
    return _contactTextFieldBgView;
}

- (void)clickRelateArrowButton {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickRelateTextField:)]) {
        [self.delegate didClickRelateTextField:self.contactCircumibilityModel];
    }
}

- (void)clickContactArrowButton {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClickContactTextField:)]) {
        [self.delegate didClickContactTextField:self.contactCircumibilityModel];
    }
}

- (void)setContactCircumibilityModel:(ContactCircumibilityModel *)contactCircumibilityModel {
    _contactCircumibilityModel = contactCircumibilityModel;
    
    self.titleLabel.text = contactCircumibilityModel.leukfier;
    self.relateTextField.placeholder = contactCircumibilityModel.locuto;
    NSString *relate = nil;
    for (PersonalEveryablyModel *model in contactCircumibilityModel.cruitative) {
        if ([model.tergmost isEqualToString:contactCircumibilityModel.droplike]) {
            relate = model.quindecimlocalety;
        }
    }
    self.relateTextField.text = relate;
    
    NSString *phone = nil;
    if (contactCircumibilityModel.quindecimlocalety &&
        contactCircumibilityModel.quindecimlocalety.length > 0 &&
        contactCircumibilityModel.consumeratic&&
        contactCircumibilityModel.consumeratic.length > 0) {
        phone = [NSString stringWithFormat:@"%@-%@",contactCircumibilityModel.quindecimlocalety, contactCircumibilityModel.consumeratic];;
    }
   
    self.contactTextField.placeholder = contactCircumibilityModel.industryivity;
    self.contactTextField.text = phone;
}

@end
